package org.akomantoso.core.data;

import java.io.IOException;

import org.akomantoso.core.data.attribute.Link;
import org.akomantoso.core.data.group.HTMLinline;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The element a is an HTML element and is used in Akoma Ntoso as in HTML, for the generic link to a web resource (NOT
 * to an Akoma Ntoso document: use ref for that). It is an inline.
 * 
 * <pre>
 * <xsd:element name="a">
 * 		<xsd:complexType mixed="true">
 * 			<xsd:complexContent>
 * 				<xsd:extension base="inline">
 * 					<xsd:attributeGroup ref="link"/>
 * 					<xsd:attribute name="target" type="xsd:string"/>
 * 				</xsd:extension>
 *			</xsd:complexContent>
 *		</xsd:complexType>
 * </xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public final class A extends InlineType implements HTMLinline , Link {

	public static final String ELEMENT = "a";
	
	public static final String ATTRIBUTE_TARGET = "target";
	
	// Mandatory
	private String _href;
	
	// Optional
	private String _target;
	
	/**
	 * @see org.akomantoso.core.data.attribute.Link#getHref()
	 */
	public String getHref() {
		return _href;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Link#setHref(java.lang.String)
	 */
	public void setHref(String href) {
		_href = href; 
	}

	public String getTarget() {
		return _target;
	}

	public void setTarget(String target) {
		_target = target;
	}

	/**
	 * @see org.akomantoso.core.data.InlineType#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <E extends Output> void writeExternal(E out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeLink(out, this);
		OutputUtil.writeAttribute(out, ATTRIBUTE_TARGET, _target);
		super.writeExternal(out);
		out.writeEnd();
	}
	
	/**
	 * @see org.akomantoso.core.data.InlineType#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <E extends Input> void readExternal(E in) throws IOException {
		_href = in.readAttribute(Link.ATTRIBUTE);
		_target = in.readAttribute(ATTRIBUTE_TARGET);
		super.readExternal(in);
	}

	/**
	 * @see ec.ep.visitor.AKVisitable#accept(ec.ep.visitor.AKVisitor)
	 */
	public void accept(AKVisitor visitor) {
		if (visitor.visitBegin(this)) {
			internalAccept(visitor);
			visitor.visitEnd(this);
		}
	}
	
}